<?php
namespace Api\Controller;
use Think\Controller;
class ShopController extends CommonController {
	public function shopTypeList(){
		$shoptype = M ('Shoptype');
		$res = $shoptype->select();
		$data = array ();
		foreach( $res as $val)
		{	
			$Shop = M ('Shop');
			$shoptype_id = $val['shoptype_id'];
			$title = $Shop->where("shoptype_id='{$shoptype_id}'")->select();
			$val['title']=$title;
			$data[]= $val;
		}	
		if( $data == false ){
			$this->response(false,0,"未知错误",true);
		}else{
			$this->response(true,1,$data,true);
		}
		
	}
	public function newaddr(){
		$addr_id = I ('get.addr_id');
		$user_nickname = I ('get.user_nickname');
		$mobile_no = I ('get.mobile_no');
		$province = I ('get.province');
		$city = I ('get.city');
		$area = I ('get.area');
		$addr = I ('get.addr');
		$Address = M ("Address");
		//1.需要判断值是否为空，为空不能提交
		//2.地址相同不能重复添加
		//3.符合条件才能进入数据库
		$data = array(
			'user_nickname' => $user_nickname,
			'mobile_no' => $mobile_no,
			'province' => $province,
			'city' => $city,
			'area' => $area,
			'addr' => $addr,
			'addtime' => time(),
			'user_id' => $_SESSION['user_id']
		);
		if( $addr_id != ' '  ){
			$Address->data($data)->where("addr_id={$addr_id}")->save($data);
			$this->response(true,1,"修改成功",true);
			var_dump(1111);
		}else{
			$res = $Address->add($data);
			if( $res == false){
				$this->response(false,0,"未知错误",true);
			}else{
				$this->response(true,2,"添加成功",true);
			}
		}
	}
	public function shopdetial(){
		$shop_id = I ('get.shop_id');
		$Shop = M ('Shop');
		$res = $Shop->where("shop_id={$shop_id}")->find();
		$kouwei = $res['kouwei'];
		$arr = explode("/",$kouwei);
		$res['kouwei'] = $arr;
		if( $res == false){
			$this->response(false,0,"不存在该商品",true);
		}else{
			$this->response(true,1,$res,true);
		}
	}
	public function sudubuy(){
		$shop_id = I ('get.shop_id');
		$count = I ('get.count');
		$kouwei = I ('get.kouwei');
		
		//将‘口味：’和具体组成数组
		$Shop = M ('Shop');
		$ret = $Shop->where("shop_id={$shop_id}")->find();
		$kouweiarr = $ret['kouwei'];
		$arr = explode("/",$kouweiarr);
		$ret['kouwei'] = $arr;
		//$kw =array($ret['kouwei'][0],$kouwei);//$kouwei = "口味：（口味值）"
		$kw =$ret['kouwei'][0].'/'.$kouwei;//$kouwei = "口味：/（口味值）"
		$data = array(
			'user_id' => $_SESSION['user_id'],
			'shop_id' => $shop_id,
			'count' => $count,
			'kouwei'=>$kw,
			'num'=>$ret['count']*$ret['cxprice'],
			'addtime' => time()
		);
		$shopcart = M ('Shopcart');
		$res = $shopcart->add($data);
		if( $res == false){
			$this->response(false,0,"立即购买失败",true);
		}else{
			$this->response(true,1,$data,true);
		}
	}
	//点击‘加入购物车’后的操作
	public function buycar(){
		$shop_id = I ('get.shop_id');
		$count = I ('get.count');
		$kouwei = I ('get.kouwei');
		//将‘口味：’和具体组成数组
		$Shop = M ('Shop');
		$ret = $Shop->where("shop_id={$shop_id}")->find();
		$kouweiarr = $ret['kouwei'];
		$arr = explode("/",$kouweiarr);
		$ret['kouwei'] = $arr;
		$kw =$ret['kouwei'][0].'/'.$kouwei;//$kouwei = "口味：/（口味值）"
		$data = array(
			'user_id' => $_SESSION['user_id'],
			'shop_id' => $shop_id,
			'count' => $count,
			'kouwei'=>$kw,
			'num'=>$ret['count']*$ret['cxprice'],
			'addtime' => time()
		);
		$shopcart = M ('Shopcart');
		$res = $shopcart->add($data);
		if( $res == false){
			$this->response(false,0,"加入购物车失败",true);
		}else{
			
			$this->response(true,1,$res,true);
		}
	}
	//购物车内操作
	public function shopcart(){
		//$shop_id = I ('get.shop_id');
		$user_id = $_SESSION['user_id'];		
		$Shopcart = M ('Shopcart');
		$res = $Shopcart->where("user_id={$user_id} and status = 0")->select();
		$data =array();
		foreach ( $res as $val)
		{
			$Shop = M ('Shop');
			$list = $Shop->where("shop_id={$val['shop_id']}")->find();
			$picture = $list['picture'];
			$var = json_decode($picture);
			$arr = explode("/",$val['kouwei']);
			$val['picture']=$var[0];
			$val['title']=$list['title'];
			$val['kouwei']=$arr;
			$val['cxprice']=$list['cxprice'];
			$val['price']=$list['price'];
			$val['num']=$val['count']*$list['cxprice'];
			$data[]=$val;
		}
		if( $data == false){
			$this->response(false,0,"加载错误",true);
		}else{
			$this->response(true,1,$data,true);
		}
	}
	public function delsc(){
		$shopcart_id = I ('get.shopcart_id');
		$Shopcart = M ('Shopcart');
		$res = $Shopcart->where("shopcart_id={$shopcart_id}")->delete();
		if( $res == false){
			$this->response(false,0,"删除失败",true);
		}else{
			$this->response(true,1,$res,true);
		}
	} 
	public function reduce(){
		$shopcart_id = I ('get.shopcart_id');
		$count = I ('get.count');
		
		$Shopcart = M ('Shopcart');
		$res = $Shopcart->where("shopcart_id={$shopcart_id}")->find();
		$Shop = M ('Shop');
		$ret = $Shop->where("shop_id={$res['shop_id']}")->find();
		
		$num = $count*$ret['cxprice'];
		$data=array(
			'count' => $count,
			'num' => $count*$ret['cxprice']
		);
		$list = $Shopcart->where("shopcart_id={$shopcart_id}")->save($data);
		if( $list == false){
			$this->response(false,0,"未知错误",true);
		}else{
			$this->response(true,1,$list,true);
		}
	}
	public function add(){
		$shopcart_id = I ('get.shopcart_id');
		$count = I ('get.count');
		$Shopcart = M ('Shopcart');
		$res = $Shopcart->where("shopcart_id={$shopcart_id}")->find();
		$Shop = M ('Shop');
		$ret = $Shop->where("shop_id={$res['shop_id']}")->find();
		
		$num = $count*$ret['cxprice'];
		$data=array(
			'count' => $count,
			'num' => $count*$ret['cxprice']
		);
		$list = $Shopcart->where("shopcart_id={$shopcart_id}")->save($data);
		if( $list == false){
			$this->response(false,0,"未知错误",true);
		}else{
			$this->response(true,1,$list,true);
		}
	}
	public function cbbox(){
		$shopcart_id = I ('get.shopcart_id');
		$Shopcart = M ('Shopcart');
		$res = $Shopcart->where("shopcart_id={$shopcart_id}")->find();
		if($res['selected'] == '0'){
			$data=array(
				'selected' => 1,
			);
		}else if($res['selected'] == '1'){
			$data=array(
				'selected' => 0,
			);
		}
		$list = $Shopcart->where("shopcart_id={$shopcart_id}")->save($data);
		if( $list == false){
			$this->response(false,0,"未知错误",true);
		}else{
			$this->response(true,1,$list,true);
		}
	}
}
